package com.fxx.face.http.http;




import com.fxx.face.http.inteface.RequestApi;
import com.google.gson.Gson;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * @ProjectName: HttpRequestLibs
 * @Package: com.fxx.http.libs.http
 * @ClassName: HttpModel
 * @Description: Model 封装
 * @Author: 付小歆
 * @CreateDate: 2021/12/10 11:13
 */
public class HttpModel {

    private static RequestApi api = HttpServiceManager.getInstance().create(RequestApi.class);

    /**
     * 获取token
     *
     * @param grant_type
     * @param client_id
     * @param client_secret
     * @param observer
     */
    public static void getToken(String grant_type, String client_id,
                                String client_secret, Observer<ResponseBody> observer) {
        setSubscribe(api.getToken(grant_type, client_id, client_secret), observer);
    }

    /**
     * 人脸注册
     * @param observer   回调
     */
    public static void registerFace(Map<String, String> map, Observer<ResponseBody> observer) {
        setSubscribe(api.registerFace(map,"application/json;charset=UTF-8"),observer);
    }

    /**
     * 验证人脸
     * @param map
     * @param observer
     */
    public static void verifyFace(Map<String,String> map,Observer<ResponseBody> observer){
        setSubscribe(api.verifyFace(map,"application/json;charset=UTF-8"),observer);
    }

    /**
     * 删除人脸
     * @param map
     * @param observer
     */
    public static void deleteFace(Map<String,String> map,Observer<ResponseBody> observer){
        setSubscribe(api.deleteFace(map,"application/json;charset=UTF-8"),observer);
    }

    /**
     * 更新人脸
     * @param map
     * @param observer
     */
    public static void updateFace(Map<String,String> map,Observer<ResponseBody> observer){
        setSubscribe(api.updateFace(map,"application/json;charset=UTF-8"),observer);
    }

    private static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) {
        observable.subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())    //子线程访问网络
                .observeOn(AndroidSchedulers.mainThread())  //回调主线程
                .subscribe(observer);
    }

    private static String obj2Json(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }
}